/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBuscarAlumno.java
 *
 * Created on 16/10/2014, 10:11:54 PM
 */
package mx.com.ugto.caadi.asistencias.gui;

import java.util.List;
import mx.com.ugto.caadi.asistencias.bean.Alumno;
import mx.com.ugto.caadi.asistencias.dao.AlumnoDAO;
import mx.com.ugto.caadi.asistencias.gui.model.ModeloTablaAlumnos;

/**
 *
 * @author Rolando
 */
public class PanelBuscarAlumno extends javax.swing.JPanel {

    private ModeloTablaAlumnos modeloTabla;

    /** Creates new form PanelBuscarAlumno */
    public PanelBuscarAlumno() {
        initComponents();
        modeloTabla = new ModeloTablaAlumnos();
        tblAlumnos.setModel(modeloTabla);
    }

    void cargarDatosTabla() {
        modeloTabla = new ModeloTablaAlumnos();
        List<Alumno> lstAlumnos;
        AlumnoDAO dao = new AlumnoDAO();
        lstAlumnos = dao.findAll();
        for (Alumno alumno : lstAlumnos) {
            modeloTabla.agregarAlumno(alumno);
        }
        tblAlumnos.setModel(modeloTabla);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMenu = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        scrAlumnos = new javax.swing.JScrollPane();
        tblAlumnos = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        pnlMenu.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("mx/com/ugto/caadi/asistencias/resources/vista"); // NOI18N
        btnNuevo.setText(bundle.getString("boton.nuevo")); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        pnlMenu.add(btnNuevo);

        btnEditar.setText(bundle.getString("boton.editar")); // NOI18N
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        pnlMenu.add(btnEditar);

        add(pnlMenu, java.awt.BorderLayout.PAGE_START);

        tblAlumnos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrAlumnos.setViewportView(tblAlumnos);

        add(scrAlumnos, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
// TODO add your handling code here:
    Main.getInstance().registrarAlumno();
}//GEN-LAST:event_btnNuevoActionPerformed

private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
// TODO add your handling code here:
    int filaSeleccionada = tblAlumnos.getSelectedRow();
    Integer idAlumno = Integer.parseInt("" + modeloTabla.getValueAt(filaSeleccionada, 10));
    if (idAlumno >= 0) {
        AlumnoDAO dao = new AlumnoDAO();
        Alumno alumno = dao.find(idAlumno);
        Main.getInstance().editarAlumno(alumno);
    }
}//GEN-LAST:event_btnEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JPanel pnlMenu;
    private javax.swing.JScrollPane scrAlumnos;
    private javax.swing.JTable tblAlumnos;
    // End of variables declaration//GEN-END:variables
}
